<?php

/**
 * Created by IntelliJ IDEA.
 * User: jirapatjantsakool
 * Date: 3/7/14
 * Time: 12:24 AM
 */

require('../db/database.php');
require('../service/common_service.php');

class configure_service extends common_service
{

    public function findAll()
    {
        global $db;
        $sql = "SELECT * FROM go_configure";
        $query = $db->query($sql);

        $configure_list = array();

        while ($result = $db->fetch_array($query)) {

            $configure = new configure();

            $configure->setId($result['id']);
            $configure->setName($result['name']);
            $configure->setContent($result['content']);

            $configure_list[] = $configure;
        }
        return $configure_list;
    }

    public function findConfigureByName($name)
    {
        global $db;
        $configure = new configure();

        $sql = "SELECT "
            . " id, name, content "
            . " FROM go_configure "
            . " WHERE name = ? ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 's', $name);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['id'],
                                           $rowMaps['name'],
                                           $rowMaps['content']);

            //TODO check number record
            if ($db->fetch_statement($stmt)) {

                $configure->setId($rowMaps['id']);
                $configure->setName($rowMaps['name']);
                $configure->setContent($rowMaps['content']);

            }
            $db->close_connection();
        }
        return $configure;
    }

    public function delete($configure)
    {
        global $db;
        static $flag = false;

        $sql = "DELETE FROM go_configure "
            . " WHERE name = ? ";

        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 's', $configure->getName());

            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }

    public function update($configure)
    {
        global $db;
        static $flag = false;

        $sql = "UPDATE go_configure SET content = ? "
            . " WHERE name = ? ";

        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'ss', $configure->getContent(),
                $configure->getName());

            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }

    public function create($cofigure)
    {
        global $db;
        static $flag = false;

        $sql = "INSERT INTO go_configure"
            . "(name, content) "
            . " VALUES( ?, ?) ";


        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'ss', $cofigure->getName(),
                $cofigure->getContent());
            $flag = $db->execute_statement($stmt);
        }

        $db->close_connection();
        return $flag;
    }

    public function isValidateTokenKey($token)
    {
        global $db;
        $common_service = new common_service();
        return $common_service->validateTokenKey($db, $token, 'token');
    }

}

$configure_service = new configure_service();
//var_dump($configure_service->isValidateTokenKey('abc1234'));

?>
